﻿namespace AnyOffice.DAL
{
    using AnyOffice.Model;
    using Microsoft.ApplicationBlocks.Data;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class VehicleUsageDao
    {
        public bool Delete(SqlConnection conn, int usageId, string userId, bool isAdministrator)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("IF @IsAdministrator =1 ");
            builder.Append("\tDELETE FROM [dbo].[Vehicle_Usage] ");
            builder.Append("\tWHERE ");
            builder.Append("\t\t[UsageId] = @UsageId ");
            builder.Append("ELSE\t ");
            builder.Append("\tDELETE FROM [dbo].[Vehicle_Usage] ");
            builder.Append("\tWHERE ");
            builder.Append("\t\t[UsageId] = @UsageId ");
            builder.Append("\t\tAND [Proposer] = @UserId ");
            builder.Append("\t\tAND Status IN(0,2) ");
            SqlParameter[] commandParameters = new SqlParameter[3];
            commandParameters[0] = new SqlParameter("@UsageId", SqlDbType.Int);
            commandParameters[0].Value = usageId;
            commandParameters[1] = new SqlParameter("@UserId", SqlDbType.NVarChar, 20);
            commandParameters[1].Value = userId;
            commandParameters[2] = new SqlParameter("@IsAdministrator", SqlDbType.Bit);
            commandParameters[2].Value = isAdministrator;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, builder.ToString(), commandParameters);
            return true;
        }

        public int Insert(SqlConnection conn, VehicleUsageData data)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("INSERT INTO [dbo].[Vehicle_Usage] ");
            builder.Append("( ");
            builder.Append("\t[VehicleId], ");
            builder.Append("\t[Driver], ");
            builder.Append("\t[Person], ");
            builder.Append("\t[DeptId], ");
            builder.Append("\t[StartTime], ");
            builder.Append("\t[EndTime], ");
            builder.Append("\t[Destination], ");
            builder.Append("\t[Mileage], ");
            builder.Append("\t[Proposer], ");
            builder.Append("\t[Operator], ");
            builder.Append("\t[Reason], ");
            builder.Append("\t[Remark], ");
            builder.Append("\t[RequestTime], ");
            builder.Append("\t[SmsRemind], ");
            builder.Append("\t[MobileRemind], ");
            builder.Append("\t[Status] ");
            builder.Append(") ");
            builder.Append("VALUES ");
            builder.Append("( ");
            builder.Append("\t@VehicleId, ");
            builder.Append("\t@Driver, ");
            builder.Append("\t@Person, ");
            builder.Append("\t@DeptId, ");
            builder.Append("\t@StartTime, ");
            builder.Append("\t@EndTime, ");
            builder.Append("\t@Destination, ");
            builder.Append("\t@Mileage, ");
            builder.Append("\t@Proposer, ");
            builder.Append("\t@Operator, ");
            builder.Append("\t@Reason, ");
            builder.Append("\t@Remark, ");
            builder.Append("\t@RequestTime, ");
            builder.Append("\t@SmsRemind, ");
            builder.Append("\t@MobileRemind, ");
            builder.Append("\t@Status ");
            builder.Append(") ");
            builder.Append("SELECT SCOPE_IDENTITY() AS RetValue ");
            SqlParameter[] commandParameters = new SqlParameter[0x10];
            commandParameters[0] = new SqlParameter("@VehicleId", SqlDbType.Int);
            commandParameters[0].Value = data.VehicleId;
            commandParameters[1] = new SqlParameter("@Driver", SqlDbType.NVarChar, 20);
            if (data.DriverIsNull)
            {
                commandParameters[1].Value = DBNull.Value;
            }
            else
            {
                commandParameters[1].Value = data.Driver;
            }
            commandParameters[2] = new SqlParameter("@Person", SqlDbType.NVarChar, 20);
            if (data.PersonIsNull)
            {
                commandParameters[2].Value = DBNull.Value;
            }
            else
            {
                commandParameters[2].Value = data.Person;
            }
            commandParameters[3] = new SqlParameter("@DeptId", SqlDbType.Int);
            if (data.DeptIdIsNull)
            {
                commandParameters[3].Value = DBNull.Value;
            }
            else
            {
                commandParameters[3].Value = data.DeptId;
            }
            commandParameters[4] = new SqlParameter("@StartTime", SqlDbType.DateTime);
            if (data.StartTimeIsNull)
            {
                commandParameters[4].Value = DBNull.Value;
            }
            else
            {
                commandParameters[4].Value = data.StartTime;
            }
            commandParameters[5] = new SqlParameter("@EndTime", SqlDbType.DateTime);
            if (data.EndTimeIsNull)
            {
                commandParameters[5].Value = DBNull.Value;
            }
            else
            {
                commandParameters[5].Value = data.EndTime;
            }
            commandParameters[6] = new SqlParameter("@Destination", SqlDbType.NVarChar, 200);
            if (data.DestinationIsNull)
            {
                commandParameters[6].Value = DBNull.Value;
            }
            else
            {
                commandParameters[6].Value = data.Destination;
            }
            commandParameters[7] = new SqlParameter("@Mileage", SqlDbType.Decimal);
            commandParameters[7].Precision = 10;
            commandParameters[7].Scale = 2;
            if (data.MileageIsNull)
            {
                commandParameters[7].Value = DBNull.Value;
            }
            else
            {
                commandParameters[7].Value = data.Mileage;
            }
            commandParameters[8] = new SqlParameter("@Proposer", SqlDbType.NVarChar, 20);
            commandParameters[8].Value = data.Proposer;
            commandParameters[9] = new SqlParameter("@Operator", SqlDbType.NVarChar, 20);
            commandParameters[9].Value = data.Operator;
            commandParameters[10] = new SqlParameter("@Reason", SqlDbType.NText);
            if (data.ReasonIsNull)
            {
                commandParameters[10].Value = DBNull.Value;
            }
            else
            {
                commandParameters[10].Value = data.Reason;
            }
            commandParameters[11] = new SqlParameter("@Remark", SqlDbType.NText);
            if (data.RemarkIsNull)
            {
                commandParameters[11].Value = DBNull.Value;
            }
            else
            {
                commandParameters[11].Value = data.Remark;
            }
            commandParameters[12] = new SqlParameter("@RequestTime", SqlDbType.DateTime);
            commandParameters[12].Value = data.RequestTime;
            commandParameters[13] = new SqlParameter("@SmsRemind", SqlDbType.Bit);
            commandParameters[13].Value = data.SmsRemind;
            commandParameters[14] = new SqlParameter("@MobileRemind", SqlDbType.Bit);
            commandParameters[14].Value = data.MobileRemind;
            commandParameters[15] = new SqlParameter("@Status", SqlDbType.Int);
            commandParameters[15].Value = data.Status;
            object obj2 = SqlHelper.ExecuteScalar(conn, CommandType.Text, builder.ToString(), commandParameters);
            if (obj2 != null)
            {
                return Convert.ToInt32(obj2);
            }
            return -1;
        }

        public bool Manage(SqlConnection conn, int usageId, int operation)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("UPDATE [dbo].[Vehicle_Usage] ");
            builder.Append("SET ");
            builder.Append("\t[Status] = @Operation ");
            builder.Append("WHERE ");
            builder.Append("\t[UsageId] = @UsageId ");
            SqlParameter[] commandParameters = new SqlParameter[2];
            commandParameters[0] = new SqlParameter("@UsageId", SqlDbType.Int);
            commandParameters[0].Value = usageId;
            commandParameters[1] = new SqlParameter("@Operation", SqlDbType.Int);
            commandParameters[1].Value = operation;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, builder.ToString(), commandParameters);
            return true;
        }

        public DataTable SelectByUsageId(SqlConnection conn, int usageId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT ");
            builder.Append("\t[Vehicle_Usage].[UsageId], ");
            builder.Append("\t[Vehicle_Usage].[VehicleId],\t ");
            builder.Append("\t[Vehicle_Usage].[Driver], ");
            builder.Append("\t[Vehicle_Usage].[Person], ");
            builder.Append("\t[Vehicle_Usage].[DeptId], ");
            builder.Append("\tdbo.GetDeptNames(CAST([Vehicle_Usage].[DeptId] AS nvarchar(2000))) AS DeptName,\t ");
            builder.Append("\t[Vehicle_Usage].[StartTime], ");
            builder.Append("\t[Vehicle_Usage].[EndTime], ");
            builder.Append("\t[Vehicle_Usage].[Destination], ");
            builder.Append("\t[Vehicle_Usage].[Mileage], ");
            builder.Append("\t[Vehicle_Usage].[Proposer], ");
            builder.Append("\tdbo.GetUserName([Proposer]) AS ProposerName, ");
            builder.Append("\t[Vehicle_Usage].[Operator], ");
            builder.Append("\tdbo.GetUserName([Operator]) AS OperatorName, ");
            builder.Append("\t[Vehicle_Usage].[Reason], ");
            builder.Append("\t[Vehicle_Usage].[Remark], ");
            builder.Append("\t[Vehicle_Usage].[RequestTime], ");
            builder.Append("\t[Vehicle_Usage].[SmsRemind], ");
            builder.Append("\t[Vehicle_Usage].[MobileRemind], ");
            builder.Append("\t[Vehicle_Usage].[Status], ");
            builder.Append("\t[Vehicle].[Number] ");
            builder.Append("FROM ");
            builder.Append("\t[dbo].[Vehicle_Usage], ");
            builder.Append("\t[dbo].[Vehicle] ");
            builder.Append("\t ");
            builder.Append("WHERE ");
            builder.Append("\t[Vehicle_Usage].[UsageId] = @UsageId ");
            builder.Append("\tAND [Vehicle_Usage].[VehicleId] = [Vehicle].[VehicleId] ");
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@UsageId", SqlDbType.Int) };
            commandParameters[0].Value = usageId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, builder.ToString(), commandParameters);
            set.Tables[0].TableName = "Vehicle_Usage";
            return set.Tables[0];
        }

        public DataTable SelectNoAll(SqlConnection conn, string userId, bool isAdministrator)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT ");
            builder.Append("\t[Vehicle_Usage].[UsageId], ");
            builder.Append("\t[Vehicle_Usage].[VehicleId], ");
            builder.Append("\t[Vehicle].[Number],\t ");
            builder.Append("\t[Vehicle_Usage].[Person], ");
            builder.Append("\t[Vehicle_Usage].[Reason], ");
            builder.Append("\t[Vehicle_Usage].[StartTime], ");
            builder.Append("\t[Vehicle_Usage].[EndTime], ");
            builder.Append("\t[Vehicle_Usage].[Remark], ");
            builder.Append("\t[Vehicle_Usage].[Status] ");
            builder.Append("FROM ");
            builder.Append("\t[dbo].[Vehicle_Usage], ");
            builder.Append("\t[dbo].[Vehicle] ");
            builder.Append("WHERE ");
            builder.Append("\t ([Vehicle_Usage].[Proposer]=@UserId OR @IsAdministrator=1) ");
            builder.Append("\tAND [Vehicle_Usage].[VehicleId] = [Vehicle].[VehicleId]\t ");
            builder.Append("\tAND [Vehicle_Usage].[Status] = 2 ");
            builder.Append("ORDER BY ");
            builder.Append("\t[Vehicle_Usage].[UsageId] DESC ");
            SqlParameter[] commandParameters = new SqlParameter[2];
            commandParameters[0] = new SqlParameter("@UserId", SqlDbType.NVarChar, 20);
            commandParameters[0].Value = userId;
            commandParameters[1] = new SqlParameter("@IsAdministrator", SqlDbType.Bit);
            commandParameters[1].Value = isAdministrator;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, builder.ToString(), commandParameters);
            set.Tables[0].TableName = "Vehicle_Usage";
            return set.Tables[0];
        }

        public DataTable SelectNowAll(SqlConnection conn, string userId, bool isAdministrator)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT ");
            builder.Append("\t[Vehicle_Usage].[UsageId], ");
            builder.Append("\t[Vehicle_Usage].[VehicleId], ");
            builder.Append("\t[Vehicle].[Number],\t ");
            builder.Append("\t[Vehicle_Usage].[Person], ");
            builder.Append("\t[Vehicle_Usage].[Reason], ");
            builder.Append("\t[Vehicle_Usage].[StartTime], ");
            builder.Append("\t[Vehicle_Usage].[EndTime], ");
            builder.Append("\t[Vehicle_Usage].[Remark], ");
            builder.Append("\t[Vehicle_Usage].[Status] ");
            builder.Append("FROM ");
            builder.Append("\t[dbo].[Vehicle_Usage], ");
            builder.Append("\t[dbo].[Vehicle] ");
            builder.Append("WHERE ");
            builder.Append("\t ([Vehicle_Usage].[Proposer]=@UserId OR @IsAdministrator=1) ");
            builder.Append("\tAND [Vehicle_Usage].[VehicleId] = [Vehicle].[VehicleId] ");
            builder.Append("\tAND [Vehicle_Usage].[Status] = 1\t\t ");
            builder.Append("\tAND [Vehicle_Usage].[StartTime] < GETDATE() ");
            builder.Append("\tAND [Vehicle_Usage].[EndTime] > GETDATE() ");
            builder.Append("ORDER BY ");
            builder.Append("\t[Vehicle_Usage].[UsageId] DESC ");
            SqlParameter[] commandParameters = new SqlParameter[2];
            commandParameters[0] = new SqlParameter("@UserId", SqlDbType.NVarChar, 20);
            commandParameters[0].Value = userId;
            commandParameters[1] = new SqlParameter("@IsAdministrator", SqlDbType.Bit);
            commandParameters[1].Value = isAdministrator;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, builder.ToString(), commandParameters);
            set.Tables[0].TableName = "Vehicle_Usage";
            return set.Tables[0];
        }

        public DataTable SelectPaged(SqlConnection conn, int pagesize, int pageindex, bool docount, string userId, bool isAdministrator)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("IF(@docount=1) ");
            builder.Append("\tSELECT COUNT([UsageId]) FROM [dbo].[Vehicle_Usage] WHERE [Status]=1 AND ([Proposer]=@UserId OR @IsAdministrator=1) ");
            builder.Append("ELSE ");
            builder.Append("\tBEGIN ");
            builder.Append("\t\tDECLARE @IndexTable TABLE(ID int identity(1,1),PK int) ");
            builder.Append("\t\tDECLARE @PageLowerBound int ");
            builder.Append("\t\tDECLARE @PageUpperBound int ");
            builder.Append("\t\tSET @PageLowerBound=(@pageindex-1)*@pagesize ");
            builder.Append("\t\tSET @PageUpperBound=@PageLowerBound+@pagesize ");
            builder.Append("\t\tSET rowcount @PageUpperBound ");
            builder.Append("\t\t ");
            builder.Append("\t\tINSERT INTO @IndexTable(PK) SELECT [UsageId] FROM [dbo].[Vehicle_Usage] WHERE [Status]=1 AND  ([Proposer]=@UserId OR @IsAdministrator=1) ORDER BY [UsageId] DESC ");
            builder.Append("\t\t ");
            builder.Append("\t\tSELECT ");
            builder.Append("\t\t\t[Vehicle_Usage].[UsageId], ");
            builder.Append("\t\t\t[Vehicle_Usage].[VehicleId], ");
            builder.Append("\t\t\t[Vehicle].[Number],\t ");
            builder.Append("\t\t\t[Vehicle_Usage].[Person], ");
            builder.Append("\t\t\t[Vehicle_Usage].[Reason], ");
            builder.Append("\t\t\t[Vehicle_Usage].[StartTime], ");
            builder.Append("\t\t\t[Vehicle_Usage].[EndTime], ");
            builder.Append("\t\t\t[Vehicle_Usage].[Remark], ");
            builder.Append("\t\t\t[Vehicle_Usage].[Status], ");
            builder.Append("\t\t\t@pageindex AS pageindex ");
            builder.Append("\t\tFROM ");
            builder.Append("\t\t\t[dbo].[Vehicle_Usage], ");
            builder.Append("\t\t\t[dbo].[Vehicle], ");
            builder.Append("\t\t\t@IndexTable t\t\t\t ");
            builder.Append("\t\tWHERE ");
            builder.Append("\t\t\t[Vehicle_Usage].[VehicleId] = [Vehicle].[VehicleId]\t ");
            builder.Append("\t\t\tAND [Vehicle_Usage].[Status] = 1 ");
            builder.Append("\t\t\tAND [Vehicle_Usage].[UsageId] = t.PK\t\t\t ");
            builder.Append("\t\t\tAND t.ID>@PageLowerBound  ");
            builder.Append("\t\t\tAND t.ID<=@PageUpperBound ");
            builder.Append("\t\tORDER BY ");
            builder.Append("\t\t\t[UsageId] DESC ");
            builder.Append("\tEND ");
            SqlParameter[] commandParameters = new SqlParameter[5];
            commandParameters[0] = new SqlParameter("@pagesize", SqlDbType.Int);
            commandParameters[0].Value = pagesize;
            commandParameters[1] = new SqlParameter("@pageindex", SqlDbType.Int);
            commandParameters[1].Value = pageindex;
            commandParameters[2] = new SqlParameter("@docount", SqlDbType.Bit);
            commandParameters[2].Value = docount;
            commandParameters[3] = new SqlParameter("@UserId", SqlDbType.NVarChar, 20);
            commandParameters[3].Value = userId;
            commandParameters[4] = new SqlParameter("@IsAdministrator", SqlDbType.Bit);
            commandParameters[4].Value = isAdministrator;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, builder.ToString(), commandParameters);
            set.Tables[0].TableName = "Vehicle_Usage";
            return set.Tables[0];
        }

        public DataTable SelectPagedByVehicleId(SqlConnection conn, int pagesize, int pageindex, bool docount, int vehicleId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("IF(@docount=1) ");
            builder.Append("\tSELECT COUNT([UsageId]) FROM [dbo].[Vehicle_Usage] WHERE [VehicleId] = @VehicleId ");
            builder.Append("ELSE ");
            builder.Append("\tBEGIN ");
            builder.Append("\t\tDECLARE @IndexTable TABLE(ID int identity(1,1),PK int) ");
            builder.Append("\t\tDECLARE @PageLowerBound int ");
            builder.Append("\t\tDECLARE @PageUpperBound int ");
            builder.Append("\t\tSET @PageLowerBound=(@pageindex-1)*@pagesize ");
            builder.Append("\t\tSET @PageUpperBound=@PageLowerBound+@pagesize ");
            builder.Append("\t\tSET rowcount @PageUpperBound ");
            builder.Append("\t\t ");
            builder.Append("\t\tINSERT INTO @IndexTable(PK) SELECT [UsageId] FROM [dbo].[Vehicle_Usage] WHERE [VehicleId] = @VehicleId ORDER BY [UsageId] DESC ");
            builder.Append("\t\t ");
            builder.Append("\t\tSELECT ");
            builder.Append("\t\t\t[UsageId], ");
            builder.Append("\t\t\t[VehicleId],\t\t\t ");
            builder.Append("\t\t\t[Person], ");
            builder.Append("\t\t\t[Reason], ");
            builder.Append("\t\t\t[StartTime], ");
            builder.Append("\t\t\t[EndTime], ");
            builder.Append("\t\t\t[Remark], ");
            builder.Append("\t\t\t[Status], ");
            builder.Append("\t\t\t@pageindex AS pageindex ");
            builder.Append("\t\tFROM ");
            builder.Append("\t\t\t[dbo].[Vehicle_Usage],\t\t\t ");
            builder.Append("\t\t\t@IndexTable t\t\t\t ");
            builder.Append("\t\tWHERE ");
            builder.Append("\t\t\t[Vehicle_Usage].[UsageId] = t.PK\t\t\t ");
            builder.Append("\t\t\tAND t.ID>@PageLowerBound  ");
            builder.Append("\t\t\tAND t.ID<=@PageUpperBound ");
            builder.Append("\t\tORDER BY ");
            builder.Append("\t\t\t[UsageId] DESC ");
            builder.Append("\tEND ");
            SqlParameter[] commandParameters = new SqlParameter[4];
            commandParameters[0] = new SqlParameter("@pagesize", SqlDbType.Int);
            commandParameters[0].Value = pagesize;
            commandParameters[1] = new SqlParameter("@pageindex", SqlDbType.Int);
            commandParameters[1].Value = pageindex;
            commandParameters[2] = new SqlParameter("@docount", SqlDbType.Bit);
            commandParameters[2].Value = docount;
            commandParameters[3] = new SqlParameter("@VehicleId", SqlDbType.Int);
            commandParameters[3].Value = vehicleId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, builder.ToString(), commandParameters);
            set.Tables[0].TableName = "Vehicle_Usage";
            return set.Tables[0];
        }

        public DataTable SelectWaitAll(SqlConnection conn, string userId, bool isAdministrator)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("SELECT ");
            builder.Append("\t[Vehicle_Usage].[UsageId], ");
            builder.Append("\t[Vehicle_Usage].[VehicleId], ");
            builder.Append("\t[Vehicle].[Number],\t ");
            builder.Append("\t[Vehicle_Usage].[Person], ");
            builder.Append("\t[Vehicle_Usage].[Reason], ");
            builder.Append("\t[Vehicle_Usage].[StartTime], ");
            builder.Append("\t[Vehicle_Usage].[EndTime], ");
            builder.Append("\t[Vehicle_Usage].[Remark], ");
            builder.Append("\t[Vehicle_Usage].[Status] ");
            builder.Append("FROM ");
            builder.Append("\t[dbo].[Vehicle_Usage], ");
            builder.Append("\t[dbo].[Vehicle] ");
            builder.Append("WHERE ");
            builder.Append("\t([Vehicle_Usage].[Proposer]=@UserId OR @IsAdministrator=1) ");
            builder.Append("\tAND [Vehicle_Usage].[VehicleId] = [Vehicle].[VehicleId]\t ");
            builder.Append("\tAND [Vehicle_Usage].[Status] = 0 ");
            builder.Append("ORDER BY ");
            builder.Append("\t[Vehicle_Usage].[UsageId] DESC ");
            SqlParameter[] commandParameters = new SqlParameter[2];
            commandParameters[0] = new SqlParameter("@UserId", SqlDbType.NVarChar, 20);
            commandParameters[0].Value = userId;
            commandParameters[1] = new SqlParameter("@IsAdministrator", SqlDbType.Bit);
            commandParameters[1].Value = isAdministrator;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, builder.ToString(), commandParameters);
            set.Tables[0].TableName = "Vehicle_Usage";
            return set.Tables[0];
        }

        public bool Update(SqlConnection conn, VehicleUsageData data)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("UPDATE [dbo].[Vehicle_Usage] ");
            builder.Append("SET ");
            builder.Append("\t[VehicleId] = @VehicleId, ");
            builder.Append("\t[Driver] = @Driver, ");
            builder.Append("\t[Person] = @Person, ");
            builder.Append("\t[DeptId] = @DeptId, ");
            builder.Append("\t[StartTime] = @StartTime, ");
            builder.Append("\t[EndTime] = @EndTime, ");
            builder.Append("\t[Destination] = @Destination, ");
            builder.Append("\t[Mileage] = @Mileage, ");
            builder.Append("\t[Operator] = @Operator, ");
            builder.Append("\t[Reason] = @Reason, ");
            builder.Append("\t[Remark] = @Remark, ");
            builder.Append("\t[RequestTime] = @RequestTime, ");
            builder.Append("\t[SmsRemind] = @SmsRemind, ");
            builder.Append("\t[MobileRemind] = @MobileRemind, ");
            builder.Append("\t[Status] = @Status ");
            builder.Append("WHERE ");
            builder.Append("\t[UsageId] = @UsageId ");
            SqlParameter[] commandParameters = new SqlParameter[0x11];
            commandParameters[0] = new SqlParameter("@UsageId", SqlDbType.Int);
            commandParameters[0].Value = data.UsageId;
            commandParameters[1] = new SqlParameter("@VehicleId", SqlDbType.Int);
            commandParameters[1].Value = data.VehicleId;
            commandParameters[2] = new SqlParameter("@Driver", SqlDbType.NVarChar, 20);
            if (data.DriverIsNull)
            {
                commandParameters[2].Value = DBNull.Value;
            }
            else
            {
                commandParameters[2].Value = data.Driver;
            }
            commandParameters[3] = new SqlParameter("@Person", SqlDbType.NVarChar, 20);
            if (data.PersonIsNull)
            {
                commandParameters[3].Value = DBNull.Value;
            }
            else
            {
                commandParameters[3].Value = data.Person;
            }
            commandParameters[4] = new SqlParameter("@DeptId", SqlDbType.Int);
            if (data.DeptIdIsNull)
            {
                commandParameters[4].Value = DBNull.Value;
            }
            else
            {
                commandParameters[4].Value = data.DeptId;
            }
            commandParameters[5] = new SqlParameter("@StartTime", SqlDbType.DateTime);
            if (data.StartTimeIsNull)
            {
                commandParameters[5].Value = DBNull.Value;
            }
            else
            {
                commandParameters[5].Value = data.StartTime;
            }
            commandParameters[6] = new SqlParameter("@EndTime", SqlDbType.DateTime);
            if (data.EndTimeIsNull)
            {
                commandParameters[6].Value = DBNull.Value;
            }
            else
            {
                commandParameters[6].Value = data.EndTime;
            }
            commandParameters[7] = new SqlParameter("@Destination", SqlDbType.NVarChar, 200);
            if (data.DestinationIsNull)
            {
                commandParameters[7].Value = DBNull.Value;
            }
            else
            {
                commandParameters[7].Value = data.Destination;
            }
            commandParameters[8] = new SqlParameter("@Mileage", SqlDbType.Decimal);
            commandParameters[8].Precision = 10;
            commandParameters[8].Scale = 2;
            if (data.MileageIsNull)
            {
                commandParameters[8].Value = DBNull.Value;
            }
            else
            {
                commandParameters[8].Value = data.Mileage;
            }
            commandParameters[9] = new SqlParameter("@Proposer", SqlDbType.NVarChar, 20);
            commandParameters[9].Value = data.Proposer;
            commandParameters[10] = new SqlParameter("@Operator", SqlDbType.NVarChar, 20);
            commandParameters[10].Value = data.Operator;
            commandParameters[11] = new SqlParameter("@Reason", SqlDbType.NText);
            if (data.ReasonIsNull)
            {
                commandParameters[11].Value = DBNull.Value;
            }
            else
            {
                commandParameters[11].Value = data.Reason;
            }
            commandParameters[12] = new SqlParameter("@Remark", SqlDbType.NText);
            if (data.RemarkIsNull)
            {
                commandParameters[12].Value = DBNull.Value;
            }
            else
            {
                commandParameters[12].Value = data.Remark;
            }
            commandParameters[13] = new SqlParameter("@RequestTime", SqlDbType.DateTime);
            commandParameters[13].Value = data.RequestTime;
            commandParameters[14] = new SqlParameter("@SmsRemind", SqlDbType.Bit);
            commandParameters[14].Value = data.SmsRemind;
            commandParameters[15] = new SqlParameter("@MobileRemind", SqlDbType.Bit);
            commandParameters[15].Value = data.MobileRemind;
            commandParameters[0x10] = new SqlParameter("@Status", SqlDbType.Int);
            commandParameters[0x10].Value = data.Status;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, builder.ToString(), commandParameters);
            return true;
        }
    }
}

